<template>
  <div class="line-chart">
    <h4>{{ title }}</h4>
    <div class="chart">
      <chart
        ref="chartRef"
        :option="refChartOption"
        :style="{ height: chartHeight }"
        @zrender:click="renderClickHandle"
        :currentTheme="currentTheme"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed, PropType, watch, onMounted, toRefs, Component } from 'vue';
import { Chart } from '@/components';
export interface TimelineItem {
  [key: string]: string | number;
}

export default defineComponent({
  props: {
    data: {
      type: Array as PropType<TimelineItem[]>,
      default: () => [],
    },
    aidata: {
      type: Array as PropType<TimelineItem[]>,
      default: () => [],
    },
    title: {
      type: String,
      default: '',
    },
    fieldNames: {
      type: Object,
      default: () => ({
        name: 'name',
        values: ['value'],
      }),
    },
    height: {
      type: Number,
      default: 300,
    },
    legend: {
      type: Array,
      default: () => {
        return [];
      },
    },
    currentTheme: {
      type: String,
      default: 'dark',
    },
  },
  setup(props, { emit }) {
    const chartRef = ref<any>();

    const legend = computed(() => props.legend);
    const chartOption = {
      //   color: ['#38A7F0', '#E99220'],38A7F0
      tooltip: {
        trigger: 'axis',
      },
      color: ['#E99220', '#38A7F0'],
      legend: {
        data: legend,
        left: 'right',
        x2: '25px',
        icon: 'line',
      },
      grid: {
        left: '3%',
        right: '4%',
        containLabel: true,
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        axisLabel: {
          lineHeight: '25',
        },
      },
      yAxis: {
        type: 'value',
        axisTick: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        splitLine: {
          lineStyle: {
            type: 'dashed',
          },
        },
      },
      dataZoom: {},
      series: [
        {
          name: legend.value[0],
          type: 'line',
          data: [] as number[],
          showSymbol: false,
          symbolSize: 6,
          lineStyle: {
            color: '#E99220',
          },
        },
        {
          name: legend.value[1],
          type: 'line',
          data: [] as number[],
          //   showSymbol: false,
          lineStyle: {
            color: '#38A7F0',
          },
        },
      ],
    };
    legend.value.forEach((e, i) => {
      chartOption.series[i] = {
        name: e,
        type: 'line',
        symbolSize: 6,
        lineStyle: {
          color: e == '智能' ? '#E99220' : '#38A7F0',
        },
        data: [] as number[],
        showSymbol: false,
      };
    });

    const refChartOption = ref(chartOption);

    const chartHeight = computed(() => `${props.height}px`);

    const setChartOption = (data: any[]) => {
      const xAxis: string[] = [];
      interface SeriesDataMap {
        [key: string]: Array<any>;
      }
      const seriesDataMap: SeriesDataMap = {};
      data.forEach(item => {
        const name = item[props.fieldNames.name];
        props.fieldNames.values.forEach(value => {
          if (seriesDataMap[value]) {
            seriesDataMap[value].push(item[value]);
          } else {
            seriesDataMap[value] = [item[value]];
          }
        });
        xAxis.push(name);
      });
      refChartOption.value.xAxis.data = xAxis;
      Object.values(seriesDataMap).forEach(o => {
        refChartOption.value.series[1].data = o;
      });
    };
    const setChartOptionAi = (data: any[]) => {
      interface SeriesDataMap {
        [key: string]: Array<any>;
      }
      const seriesDataMap: SeriesDataMap = {};
      data.forEach(item => {
        props.fieldNames.values.forEach(value => {
          if (seriesDataMap[value]) {
            seriesDataMap[value].push(item[value]);
          } else {
            seriesDataMap[value] = [item[value]];
          }
        });
      });
      Object.values(seriesDataMap).forEach(o => {
        refChartOption.value.series[0].data = o;
      });
    };

    const renderClickHandle = e => {
      if (e?.topTarget?.type === 'Line') {
        const index = chartRef.value.getIndexByEvent(e);
        const item = props.data[index];
        emit('zrender:click', index, item);
      }
    };

    watch(
      () => props.data,
      () => {
        setChartOption(props.data);
      },
    );
    watch(
      () => props.aidata,
      () => {
        setChartOptionAi(props.aidata);
      },
    );
    onMounted(() => {
      setChartOption(props.data);
      setChartOptionAi(props.aidata);
    });
    return {
      chartHeight,
      refChartOption,
      renderClickHandle,
      chartRef,
    };
  },
  components: {
    Chart,
  },
});
</script>

<style lang="less" scoped></style>
